package com.scada.controller;

import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import com.scada.model.bean.User;
import com.scada.model.result.ResponseBean;
import com.scada.util.JsonDateValueProcessor;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

public class BaseController {
	
	
	protected Map<String, Object> showParams(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Enumeration<String> paramNames = request.getParameterNames();
		while (paramNames.hasMoreElements()) {
			String paramName = (String) paramNames.nextElement();
			String[] paramValues = request.getParameterValues(paramName);
			if (paramValues.length == 1) {
				String paramValue = paramValues[0];
				if (paramValue.trim().length() != 0) {
					map.put(paramName, paramValue);
				}
			}
		}
		return map;
	}
	
	
	public ResponseBean initResponseBean(){
		return new ResponseBean();
	}
	
	protected String toJson(Object page) {
		JsonConfig jsonConfig = new JsonConfig();

		jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
		JSONObject jsonObject = JSONObject.fromObject(page, jsonConfig);
		return jsonObject.toString();
	}
	
	protected String toJson2(Object page) {
		JsonConfig jsonConfig = new JsonConfig();

		jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor("yyyy-MM-dd"));
		
		String jsonObject = JSONArray.fromObject(page, jsonConfig).toString();
		return jsonObject;
	}
	
	public static User getCurrentUser(){
		Subject subject = SecurityUtils.getSubject();
		return (User)subject.getPrincipal();
	}
	
	
}
